'use strict';
var express = require('express');
const {  ObjectId, ObjectID } = require('mongodb');
var router = express.Router();

const short_url = require('../models/short_url')

/* GET home page. */
router.get('/', function (req, res) {
    res.render('index', { title: 'tts short url sys' });
});


router.get("/:name", async function (req, res) {
    var result = await short_url.get_short_info(req.params["name"])

    if (result) {

        if (result.exp_time.getTime() < Date.now()) {
            //超时

            if (result.status == 1) {
                //处理掉过时的连接
                await short_url.disable_short_url(req.params["name"])
            }

            res.status(404)
            res.end()
            return
        }
        if (result.status != 1) {
            res.status(404)
            res.end()
            return
        }

        res.redirect(301, result.absolute_url)
        await short_url.inser_short_jump_num(req.params["name"], 1)
        return
    } else {
        res.status(404)
        res.end()
        return
    }
    
})

module.exports = router;
